package com.hup.sokoban.service.main;

import com.hup.sokoban.view.main.MainForm;
import com.hup.utils.commons.hardware.MouseUtil;
import lombok.extern.log4j.Log4j2;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author hugan
 * @date 2020/6/3
 */
@Log4j2
public class LabelMouseGesture extends MouseAdapter {

    public static void bind(JFrame frame, MainForm form) {
        LabelMouseGesture gesture = new LabelMouseGesture(frame, form);
        form.mapLabel.addMouseListener(gesture);
        form.mapLabel.addMouseMotionListener(gesture);
    }

    private final JFrame win;
    private final MainForm form;
    /**
     * 鼠标拖拽前的坐标
     */
    private int mX0, mY0;
    /**
     * 窗口拖拽前的坐标
     */
    private int wX0, wY0;
    /**
     * 开始拖拽前,窗口是否为最大化
     */
    private boolean maxAtPressed;
    /**
     * 最大化拖拽前,鼠标在窗口中的位置比例
     */
    private float mXRatio, mYRatio;

    private LabelMouseGesture(JFrame win, MainForm mainForm) {
        this.win = win;
        this.form = mainForm;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (MouseUtil.isLeftButton(e) && e.getClickCount() == 2) {
            if (Frame.MAXIMIZED_BOTH == win.getExtendedState()) {
                win.setExtendedState(Frame.NORMAL);
            } else {
                //全屏
                win.setExtendedState(Frame.MAXIMIZED_BOTH);
                form.cbMapList.setSelected(true);
                form.cbMapList.doClick();
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (!SwingUtilities.isLeftMouseButton(e)) return;
        mX0 = e.getXOnScreen();
        mY0 = e.getYOnScreen();
        wX0 = win.getX();
        wY0 = win.getY();
        maxAtPressed = Frame.MAXIMIZED_BOTH == win.getExtendedState();
        if (maxAtPressed) {
            float width = win.getWidth();
            float height = win.getHeight();
            mXRatio = (mX0 - wX0) / width;
            mYRatio = (mY0 - wY0) / height;
            log.debug("width={}, height={}, mXRatio={}, mYRatio={}", width, height, mXRatio, mYRatio);
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (!SwingUtilities.isLeftMouseButton(e)) return;
        int x, y;
        if (Frame.MAXIMIZED_BOTH == win.getExtendedState()) {
            //最大化时,先最小化
            win.setExtendedState(Frame.NORMAL);
        }
        if (maxAtPressed) {
            int width = win.getWidth();
            int height = win.getHeight();
            log.debug("width={}, height={}", width, height);
            x = (int) (e.getXOnScreen() - width * mXRatio);
            y = (int) (e.getYOnScreen() - height * mYRatio);
        } else {
            int xDiff = e.getXOnScreen() - mX0;
            int yDiff = e.getYOnScreen() - mY0;
            x = wX0 + xDiff;
            y = wY0 + yDiff;
        }
        win.setLocation(x, y);
    }

}
